package cn.yunyichina.provider.pay.alipay.service.impl;

import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.dto.pay.AlipayBillDownloadRequestDTO;
import cn.yunyichina.provider.iface.entity.dto.pay.AlipayBillDownloadResponseDTO;
import cn.yunyichina.provider.pay.alipay.config.Alipay;
import cn.yunyichina.provider.pay.alipay.constant.ResponseEnum;
import cn.yunyichina.provider.pay.alipay.service.AlipayBillService;
import cn.yunyichina.provider.pay.alipay.service.BaseTradeService;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayDataDataserviceBillDownloadurlQueryRequest;
import com.alipay.api.response.AlipayDataDataserviceBillDownloadurlQueryResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * Created by Lullaby on 2017/7/12.
 */
@Service
public class AlipayBillServiceImpl extends BaseTradeService implements AlipayBillService {

    private static final Logger logger = LoggerFactory.getLogger(AlipayBillServiceImpl.class);

    public ResCommon<AlipayBillDownloadResponseDTO> downloadAlipayBill(AlipayBillDownloadRequestDTO downloadRequest) {
        ResCommon<AlipayBillDownloadResponseDTO> downloadResponse = new ResCommon<>();

        AlipayClient alipayClient = new DefaultAlipayClient(
                getConfig(Alipay.ALIPAY_GATEWAY_OPENAPI),
                downloadRequest.getAppId(),
                downloadRequest.getPrivateKey(),
                downloadRequest.getFormat(),
                downloadRequest.getCharset(),
                downloadRequest.getPublicKey(),
                downloadRequest.getSignType()
        );

        AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
        request.setBizContent(downloadRequest.getBizContent());
        logger.info("AlipayBillServiceImpl.downloadAlipayBill.request -> {}", JSON.toJSONString(request));

        try {
            AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient.execute(request);
            logger.info("AlipayBillServiceImpl.downloadAlipayBill.response -> {}", JSON.toJSONString(response));

            AlipayBillDownloadResponseDTO downloadResponseDTO = JsonUtils.parseObject(response.getBody(), AlipayBillDownloadResponseDTO.class);
            logger.info("AlipayBillServiceImpl.downloadAlipayBill.downloadResponseDTO -> {}", JSON.toJSONString(downloadResponseDTO));

            if (!response.isSuccess()) {
                downloadResponse.setResultCodeAndMessage(
                        ResponseEnum.ALIPAY_DOWNLOAD_BILL_FAILURE.getCode(),
                        ResponseEnum.ALIPAY_DOWNLOAD_BILL_FAILURE.getMessage()
                );
            }

            downloadResponse.setResult(downloadResponseDTO);
        } catch (AlipayApiException e) {
            logger.error("AlipayBillServiceImpl.downloadAlipayBill.AlipayApiException", e);
            downloadResponse.setResultCodeAndMessage(
                    ResponseEnum.ALIPAY_DOWNLOAD_BILL_EXCEPTION.getCode(),
                    ResponseEnum.ALIPAY_DOWNLOAD_BILL_EXCEPTION.getMessage()
            );
        }

        return downloadResponse;
    }

}
